package util

import (
	"bytes"
	"github.com/sirupsen/logrus"
	"os"
	"strings"

	_const "aicloud.cn/aicloud-operate/pkg/util/const"
)

type CustomFormatter struct {
}

func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) {
	var b *bytes.Buffer
	if entry.Buffer != nil {
		b = entry.Buffer
	} else {
		b = &bytes.Buffer{}
	}
	timestamp := entry.Time.Format("2006-01-02 15:04:05.000")

	// 自定义日志格式
	msg := entry.Message
	level := entry.Level

	// 组装日志字符串
	logString := timestamp + ` ` + strings.ToUpper(level.String()) + ` ` + msg + ``

	b.WriteString(logString)
	// 返回字节切片
	return b.Bytes(), nil
}

var (
	logger          = logrus.New()
	customFormatter = &CustomFormatter{}
	level           = GetEnvIntOrDefault(_const.LOGGER_LEVEL_ENV, "4")
)

func init() {
	logger.SetOutput(os.Stdout)
	//logger.SetFormatter(customFormatter)
	logrus.SetLevel(logrus.Level(level)) // 默认info级别日志
}

func GetLogger() *logrus.Logger {
	return logger
}
